package org.tio.sitexxx.web.server.controller.minip;


import com.alibaba.fastjson.JSON;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.http.common.HttpRequest;
import org.tio.http.server.annotation.RequestPath;
import org.tio.sitexxx.service.model.main.Consultant;
import org.tio.sitexxx.service.model.main.Member;
import org.tio.sitexxx.service.service.qj.ConsultantService;
import org.tio.sitexxx.service.service.qj.MemberService;
import org.tio.sitexxx.service.service.qj.UserService;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestPath(value = "/setting/consultant")
public class setting_consultant {
    private static Logger log = LoggerFactory.getLogger(setting_consultant.class);
    public setting_consultant(){

    }

    /**
     * 顾问登陆成功后填写姓名和电话号码
     * @param request
     * @return
     */
    @RequestPath(value = "/modify_record")
    public  Object modify_record(HttpRequest request){
        Map<String,Object> requestmap= JSON.parseObject(request.getBodyString());
        System.out.println("modify_record:"+JSON.toJSONString(requestmap));
        Map<String, Object> resultMap = new HashMap<>();
        try{
            String openid=requestmap.get("openid").toString();//用户第一次登陆后填写手机姓名
            String name=requestmap.get("name").toString();
            String mobile=requestmap.get("mobile").toString();
            String resume=requestmap.get("study").toString();
            String qualifications=requestmap.get("qualification").toString();
            String wechat=requestmap.get("wechat").toString();
            String age_scope=requestmap.get("age").toString();
            List<String>imageUrl=JSON.parseArray(requestmap.get("imageurl").toString(),String.class);

            if(openid==null||openid.length()<20){
                resultMap.put("ok",1);
                return resultMap;
            }
            final Consultant consultant=ConsultantService.me.getConsultantByOpenId(openid);
            int flag =0;
            if(consultant==null){
//            Member = new Member();//member正常情况下不可能为null
                flag=1;
            }
            consultant.setName(name);
            consultant.setMobile(mobile);
            consultant.setAgeScope(age_scope);
            consultant.setQualifications(qualifications);
            consultant.setWechat(wechat);
            consultant.setResume(resume);
           if(imageUrl.size()==3){
               consultant.setWorks1(imageUrl.get(0));
               consultant.setWorks2(imageUrl.get(1));
               consultant.setWorks3(imageUrl.get(2));
           }
            if(flag==0){
                Db.tx(new IAtom() {
                    @Override
                    public boolean run() throws SQLException {
                        UserService.me.update(openid,name);
                        consultant.update();
                        ConsultantService.me.update_staff_name(name,openid);
                        return true;
                    }
                });
                resultMap.put("ok",0);
            }
            else {
                resultMap.put("ok",1);
                System.out.println("用户不存在");
            }
        }catch (NullPointerException e){
            log.error(e.toString(),e);
            e.printStackTrace();
            resultMap.put("ok","参数错误");
        }catch (Exception e){
            log.error(e.toString(),e);
            e.printStackTrace();
            resultMap.put("ok",1);
        }
        return resultMap;
    }

}
